-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
221 lines (187 loc) · 5.27 KB
/
code.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Model and image texture
let astronaut;
let venus;
// Material type radio element
let materialType;
// Color selector elements
let fillStrokeSelectionContainer;
let ambientSpecularSelectionContainer;
let fillCheckbox, strokeCheckbox, ambientCheckbox, specularCheckbox;
let emissivePicker;
// Selected colors
let fillSelection, strokeSelection, ambientSelection, specularSelection;
// Load astronaut model and venus image texture
function preload() {
astronaut = loadModel('assets/astronaut.obj');
venus = loadImage('assets/venus.jpg');
}
function setup() {
createCanvas(400, 400, WEBGL);
angleMode(DEGREES);
createSelectionArea();
describe(
'Astronaut 3D model displayed with a user-selected material using the selectors below the canvas.'
);
}
function draw() {
background(0);
// Save canvas settings
push();
// Get current material type
let currentMaterial = materialType.value();
switch (currentMaterial) {
case 'color':
applyColorMaterial();
break;
case 'emissive':
applyEmissiveMaterial();
break;
case 'normal':
applyNormalMaterial();
break;
case 'texture':
applyTextureMaterial();
break;
}
// Lights
ambientLight(128);
spotLight(255, 255, 255, 0, -height / 2, 200, 0, 0.5, -1, 30);
// Astronaut
translate(0, -25);
scale(4);
rotateZ(180);
model(astronaut);
// Restore canvas settings so that only current selections
// are applied in next frame
pop();
}
function createSelectionArea() {
// Create container for selection elements
let selectionArea = createDiv();
selectionArea.style('background', '#f0f0f0');
selectionArea.style('width', '400px');
selectionArea.style('font-family', 'sans-serif');
// Create material type radio selector
let materialTypeLabel = createElement('label', 'Material type');
materialTypeLabel.parent(selectionArea);
materialType = createRadio();
materialType.parent(materialTypeLabel);
materialType.option('color');
materialType.option('emissive');
materialType.option('normal');
materialType.option('texture');
materialType.selected('color');
// Create selectors for material colors
fillStrokeSelectionContainer = createDiv();
fillStrokeSelectionContainer.parent(selectionArea);
ambientSpecularSelectionContainer = createDiv();
ambientSpecularSelectionContainer.parent(selectionArea);
fillSelection = color(255);
fillCheckbox = createColorSelector(
'fill',
fillSelection,
true,
fillStrokeSelectionContainer
);
strokeSelection = color(0);
strokeCheckbox = createColorSelector(
'stroke',
strokeSelection,
true,
fillStrokeSelectionContainer
);
ambientSelection = color(255);
ambientCheckbox = createColorSelector(
'ambient',
ambientSelection,
false,
ambientSpecularSelectionContainer
);
specularSelection = color(255);
specularCheckbox = createColorSelector(
'specular',
specularSelection,
false,
ambientSpecularSelectionContainer
);
// Create picker for emissive material color
emissivePicker = createColorPicker(color(255));
emissivePicker.hide();
}
function createColorSelector(label, colorSelection, checked, parentElement) {
let checkbox = createCheckbox(label);
checkbox.parent(parentElement);
let picker = createColorPicker(colorSelection);
picker.parent(parentElement);
function setColor() {
let selectedColor = picker.color();
colorSelection.setRed(red(selectedColor));
colorSelection.setGreen(green(selectedColor));
colorSelection.setBlue(blue(selectedColor));
}
// When picker's color is changed, set the selector color to its value
picker.changed(setColor);
function setPickerVisibility() {
if (checkbox.checked() === true) {
picker.show();
} else {
picker.hide();
}
}
// When checkbox is checked, show the color picker
checkbox.changed(setPickerVisibility);
checkbox.checked(checked);
setPickerVisibility();
return checkbox;
}
function applyColorMaterial() {
ambientSpecularSelectionContainer.show();
emissivePicker.hide();
fillStrokeSelectionContainer.show();
applyAmbientSpecularMaterial();
// Set fill using current selection
if (fillCheckbox.checked() === true) {
fill(fillSelection);
} else {
noFill();
}
strokeCheckbox.show();
// Set stroke using current selection
if (strokeCheckbox.checked() === true) {
stroke(strokeSelection);
} else {
noStroke();
}
}
function applyEmissiveMaterial() {
noStroke();
ambientSpecularSelectionContainer.hide();
fillStrokeSelectionContainer.hide();
emissivePicker.show();
// Apply emissive material using selected color
emissiveMaterial(emissivePicker.color());
}
function applyNormalMaterial() {
ambientSpecularSelectionContainer.hide();
fillStrokeSelectionContainer.hide();
emissivePicker.hide();
// Apply normal material
normalMaterial();
}
function applyTextureMaterial() {
ambientSpecularSelectionContainer.show();
emissivePicker.hide();
fillStrokeSelectionContainer.hide();
applyAmbientSpecularMaterial();
noStroke();
// Apply texture
texture(venus);
}
function applyAmbientSpecularMaterial() {
if (ambientCheckbox.checked() === true) {
ambientMaterial(ambientSelection);
}
if (specularCheckbox.checked() === true) {
specularMaterial(specularSelection);
}
}